﻿using System;
using System.Collections.Generic;
using System.Net;
using log4net;

namespace NetworkScanner
{
    class Program
    {
        #region my private readonly variables (Konstanten)
        protected static readonly ILog log = LogManager.GetLogger(typeof(Program));
        protected static readonly bool MY_OWN_LAYOUT = false;
        protected static readonly bool MY_ORIGINAL_LAYOUT = true;
        #endregion

        #region my private static variables
        private static ConsoleColor orgBackgroundColor;
        private static string orgConsoleTitle;
        private static HelperNetworkBrowser myHelper;
        private static string myAssembyInfos;

        #endregion

        #region private methods
        /// <summary>
        /// init all private variables:
        /// </summary>
        private static void init()
        {
            //1. set all global variables:
            myHelper = new HelperNetworkBrowser();
            myAssembyInfos = myHelper.getAssembyInfos() + ": ";
            orgBackgroundColor = System.Console.BackgroundColor;
            orgConsoleTitle = System.Console.Title;

            //2. set consolen layout on my own:
            setConsolenLayout(MY_OWN_LAYOUT);
        }

        /// <summary>
        /// set the consolen layout
        /// if showOriginalLayout = true: set original layout
        /// otherwise set my own layout
        /// </summary>
        /// <param name="showOriginalLayout"></param>
        private static void setConsolenLayout(bool showOriginalLayout)
        {
            if (showOriginalLayout)
            {
                System.Console.BackgroundColor = orgBackgroundColor;
                System.Console.Title = orgConsoleTitle;
            }
            else
            {
                System.Console.BackgroundColor = ConsoleColor.DarkMagenta;
                System.Console.Title = myAssembyInfos;
            }

        }

        #endregion

        /// <summary>
        /// 1.8.2013:idee, gebe alle NetzwerkComputer aus
        /// 
        /// using log4net: http://sadi02.wordpress.com/2008/06/29/log4net-tutorial-in-c-net-how-can-i-show-log-in-a-file/
        ///  - used loglevel: error and debug
        ///  - logfile 'ConsoleApplication.log' in same folder like the application (see app.config)
        /// </summary>
        ///angaf: see http://msdn.microsoft.com/en-us/library/windows/desktop/aa370623(v=vs.85).aspx
        ///int SV_TYPE_NT = 1000 :: take any workstation or server.
        ///int SV_TYPE_WORKSTATION = 1; All workstations.
        ///int SV_TYPE_SERVER = 2; All computers that run the Server service.
        /// SV_TYPE_ALL = 0xFFFFFFFF; 4294967295:: list of all servers
        /// SV_TYPE_SERVER_NT = 8000; :: Any server that is not a domain controller.
        /// SV_TYPE_DOMAIN_CTRL = 8 :: A server that is primary domain controller.
        /// SV_TYPE_DOMAIN_BAKCTRL = 10 :: any server that is a backup domain controller.
        ///             
        /// <param name="args">
        /// Array:
        /// 1. Parameter: activator for logging, if true: logging is on
        /// 2. Parameter: not used!
        /// </param>
        static void Main(string[] args)
        {
            //1. call my private init-function:
            init();

            System.Console.WriteLine(myAssembyInfos + "starte...");
            System.Console.WriteLine("-------------------------------------------------------");

            //check first parameter: log activator:           
            if (args != null && args.Length > 0 && args[0] == "true")
            {
                //enable Logging: read the log4net-config from app.config
                log4net.Config.XmlConfigurator.Configure();
                log.Debug(myAssembyInfos + "Logging is activated");
            }
            log.Debug("starting ------------------------------------- timestamp:" + DateTime.Now);

            //get the domain:
            var domain = myHelper.GetDomainNameAndHostname();
            System.Console.WriteLine("1. untersuche den lokalen Rechner:");
            System.Console.WriteLine(" - hostname.domain=" + domain);

            //get the ip for the local computer:            
            IPAddress localIP = myHelper.getIPv4Address(myHelper.getLocalComputername());
            if (localIP != null)
            {
                System.Console.WriteLine(" - lokale IP:" + localIP.ToString());
                log.Debug(" - lokale IP:" + localIP.ToString());
            }
            else
            {
                System.Console.WriteLine(" - Attention: lokale IP not set! stopping here!");
                log.Debug(" - Attention: lokale IP not set! stopping here!");
                setConsolenLayout(MY_ORIGINAL_LAYOUT);
                return;
            }
            System.Console.WriteLine("-------------------------------------------------------");

            System.Console.WriteLine("2. getting PCNames from ListNetworkComputersAsString():");
            log.Debug("2. getting PCNames from ListNetworkComputersAsString():");
            var myList = myHelper.ListNetworkComputersAsString();
            foreach (string onePCName in myList)
            {
                System.Console.WriteLine(onePCName + ", IP:" + myHelper.getIPv4Address(onePCName));

                List<String> myUserList = myHelper.getAllUsers(onePCName);
                if (myUserList.Count > 0)
                {
                    System.Console.WriteLine("  found Users:");

                    foreach (string oneUser in myUserList)
                    {
                        System.Console.WriteLine("   " + oneUser);
                        log.Debug(oneUser);
                    }
                    System.Console.WriteLine("  ---");
                }

                List<String> myShareList = myHelper.getShares(onePCName);
                if (myShareList.Count > 0)
                {
                    System.Console.WriteLine("  found Shares:");
                    foreach (string oneShare in myShareList)
                    {
                        System.Console.WriteLine("   " + oneShare);
                        log.Debug(oneShare);
                    }
                    System.Console.WriteLine("  ---");
                }
            }
            System.Console.WriteLine("-------------------------------------------------------");
            System.Console.WriteLine("...Ende!");
            setConsolenLayout(MY_ORIGINAL_LAYOUT);
            log.Debug("end-------------------------------------------- timestamp:" + DateTime.Now);
        }

    }
}
